<template>
  <div id="userManagePage">
    <a-flex justify="space-between">
      <h2>分数管理</h2>
    </a-flex>
    <div style="margin-bottom: 16px"></div>
    <a-form layout="inline" :model="searchParams" @finish="doSearch">
      <a-form-item label="类型" name="type">
        <a-select
          v-model:value="searchParams.type"
          :options="operations"
          placeholder="请选择分数类型"
          style="min-width: 180px"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="用户id" name="userId">
        <a-input v-model:value="searchParams.userId" placeholder="用户id" allow-clear />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">搜索</a-button>
      </a-form-item>
    </a-form>
    <a-button class="add-or-decreaseScore" style="left:1530px;color: #1890ff;background: #f0f2f5" @click="doEdit">添加分数</a-button>
    <div style="margin-bottom: 16px" />
    <!-- 表格 -->
    <a-table
      :columns="columns"
      :data-source="dataList"
      :pagination="pagination"
      @change="doTableChange"
    >
      <template #bodyCell="{ column, record }">
        <!-- 审核信息 -->
        <template v-if="column.dataIndex === 'operator'">
          <div >操作员id：{{ record.operator?.id }}</div>
          <div>操作员名字：{{ record.operator?.userName }}</div>
        </template>
        <template v-else-if="column.dataIndex === 'createTime'">
          {{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template v-else-if="column.dataIndex === 'editTime'">
          {{ dayjs(record.editTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template v-else-if="column.dataIndex === 'updateTime'">
          {{ dayjs(record.updateTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template v-else-if="column.key === 'action'">
          <a-space>
            <a-button type="link"  @click="doEdit(record.id)">编辑</a-button>
            <a-button type="link"  @click="handleEditOrDelete(record.id)" danger >删除</a-button>
          </a-space>
        </template>
      </template>
    </a-table>
    <EditScoreWindow
      ref="editScoreWindowRef"
      :loading="loading"
      :req="req"
      :fetch-data="fetchData"
    ></EditScoreWindow>
  </div>
</template>

<script lang="ts" setup>
import {message} from "ant-design-vue";
import { computed, onMounted, reactive, ref } from 'vue'
import dayjs from 'dayjs'
import {
  deleteScoreUsingPost,
  getScoreByPageUsingPost,
  updateScoreUsingPost
} from '@/api/scoreController.ts'
import EditScoreWindow from '@/components/EditScoreWindow.vue'
import { deleteUserUsingPost } from '@/api/userController.ts'

const columns = [
  {
    title: 'id',
    dataIndex: 'id',
    width: 80,
  },
  {
    title: '用户id',
    dataIndex: 'userId',
  },
  {
    title: '用户名字',
    dataIndex: 'userName',
  },
  {
    title: '类型',
    dataIndex: 'type',
  },
  {
    title: '原因',
    dataIndex: 'reason',
    ellipsis: true,
  },
  {
    title: '分数',
    dataIndex: 'score',
  },
  {
    title: '操作员',
    dataIndex: 'operator',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '编辑时间',
    dataIndex: 'editTime',
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime',
  },
  {
    title: '操作',
    key: 'action',
  },
]
const operations= [{label:"加分",value: '加分'},{label:"减分",value: '减分'}];
// 数据
const dataList = ref([])
const total = ref(0)


// 搜索条件
const searchParams = reactive<API.QueryScoreRequest>({
  current: 1,
  pageSize: 10,
  sortField: 'createTime',
  sortOrder: 'descend',
})

// 分页参数
const pagination = computed(() => {
  return {
    current: searchParams.current ?? 1,
    pageSize: searchParams.pageSize ?? 10,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total) => `共 ${total} 条`,
  }
})

// 页面加载时请求一次
onMounted(() => {
  fetchData()
})

// 获取数据
const fetchData = async () => {
  const res = await getScoreByPageUsingPost({
    ...searchParams
  })
  if (res.data.data) {
    dataList.value = res.data.data.records ?? []
    total.value = res.data.data.total ?? 0
  } else {
    message.error('获取数据失败，' + res.data.message)
  }
}


// 获取数据
const doSearch = () => {
  // 重置搜索条件
  searchParams.current = 1
  fetchData()
}

// 表格变化处理
const doTableChange = (page: any) => {
  searchParams.current = page.current
  searchParams.pageSize = page.pageSize
  fetchData()
}

//唤出弹窗
const editScoreWindowRef = ref();

const loading=ref(false);
const req=ref<string>();
//添加和编辑
const doEdit = (id?: number) => {
  console.log('doEdit被调用，id是:', id);
  if(id){
    req.value = String(id);
  }else {
    req.value = undefined;
  }
  console.log('设置后的req.value是:', req.value);
  loading.value = true;
  if (editScoreWindowRef.value) {
    editScoreWindowRef.value.showModal()
  }
}

//编辑或者删除记录
const handleEditOrDelete = async (id:number ) => {
    const res = await deleteScoreUsingPost({
      id:id
  })
  if(res.data.code === 0&&res.data.data){
    message.success('操作成功');
    fetchData();
  } else {
    message.error('操作失败' + res.data.message);
  }
}




</script>
<style scoped>

</style>

